package com.bdqn.mzc.config;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.bdqn.mzc.excep.NotEnoughException;
import com.bdqn.mzc.excep.UserException;
import com.bdqn.mzc.model.ErrorCodes;
import com.bdqn.mzc.model.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice //等同于  ControllerAdvice + RestController
public class GlobalExeception {


    @ExceptionHandler(value = Throwable.class) //捕获所有异常
    public Result baseException(Throwable e){
        if (e instanceof UserException) {
            //处理抛出的用户异常逻辑
            return Result.wrapErrorResult(ErrorCodes.AUTH_ERROR,e.getMessage());
        }else if (e instanceof NotEnoughException){
            return Result.wrapErrorResult(ErrorCodes.AUTH_ERROR,e.getMessage());
        }else if (e instanceof TokenExpiredException){
            return Result.wrapErrorResult(ErrorCodes.AUTH_ERROR,e.getMessage());
        }else {
            return Result.wrapErrorResult(ErrorCodes.SYSTEM_ERROR,"未知错误!"+e.getMessage());
        }
    }
}
